package com.gym.controller.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.gym.controller.form.admin.InsertCourseForm;
import com.gym.model.dao.ConnessioneException;
import com.gym.model.dao.DAOCourse;
import com.gym.model.dao.MetodoDaoException;
import com.gym.model.dao.ViolazioneIntegritaException;
import com.gym.model.entity.Course;

public class InsertCourse extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		try {
			
			InsertCourseForm f = (InsertCourseForm) form;
		
			//dai dati del form compongo l'oggetto Trainer da inserire:
			String activity=f.getActivity();
			String note=f.getNote();
			Integer idtrainer=Integer.parseInt(f.getIdtrainer()) ;
			short maxcustomers=Short.parseShort(f.getMaxcustomers());
			short lifespan=Short.parseShort(f.getLifespan());
			double price=Double.parseDouble(f.getPrice());
			
			Course course = new Course(0, activity, note, idtrainer, maxcustomers, lifespan, price);
			
			DAOCourse dao = new DAOCourse();
			dao.insert(course);
			
			request.setAttribute("feedback", "OK, inserito il corso "+activity);
			
			return mapping.findForward("success");
		} catch (ConnessioneException | MetodoDaoException | ViolazioneIntegritaException e) {
			request.setAttribute("error", e.getMessage());
			return mapping.findForward("failure"); 
		}
	}
}